/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.das;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.portalcenter.common.enums.MenuTypeEnum;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.MenuBO;
import cn.hallele.ms.portalcenter.application.bo.MenuQueryBO;
import cn.hallele.ms.portalcenter.domain.action.menu.MenuDomainService;
import cn.hallele.ms.portalcenter.domain.domain.MenuDomain;
import cn.hallele.ms.portalcenter.domain.domain.MenuQueryDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 13:13
 */
@Slf4j
@Service
public class MenuBDDas extends ModelDas<MenuDomainService, MenuBO, MenuDomain> {
    protected MenuBDDas(MenuDomainService option) {
        super(option);
    }

    /**
     * 查询某个用户的菜单&权限
     *
     * @param roleCodes roleCodes
     * @return List
     */
    public List<MenuBO> findMenusByRoleCodes(Set<String> roleCodes) {
        return revertList(this.option.findByRoleCodeIn(roleCodes));
    }

    /**
     * 查询某个用户的菜单&权限
     *
     * @param roleCodes roleCodes
     * @param types     types
     * @return List
     */
    public List<MenuBO> findRoutesByRoleCodes(Set<String> roleCodes, List<MenuTypeEnum> types) {
        return revertList(this.option.findByRoleCodeIn(roleCodes, types));
    }

    /**
     * 获取 菜单列表
     *
     * @param condition 查询条件
     * @return 系统菜单
     */
    public List<MenuBO> getMenus(MenuQueryBO condition) {
        return revertList(this.option.getMenus(convertQuery(condition)));
    }

    /**
     * 暴露出去用的convert
     *
     * @param menuBOS menuBOS
     * @return MenuEntity
     */
    public Set<MenuDomain> convert(Set<MenuBO> menuBOS) {
        return ObjectUtil.toSet(convertList(ObjectUtil.toList(menuBOS)));
    }

    /**
     * 暴露出去用的revert
     *
     * @param menuDomains menuDomains
     * @return MenuDomain
     */
    public Set<MenuBO> revert(Set<MenuDomain> menuDomains) {
        return ObjectUtil.toSet(revertList(ObjectUtil.toList(menuDomains)));
    }

    /**
     * 将T转成E
     *
     * @param menuBO 被转换者
     * @return S 转换后者
     */
    @Override
    protected MenuDomain convert(MenuBO menuBO) {
        return ObjectUtil.map(menuBO, u -> MenuDomain.builder()
                .id(u.getId())
                .parentId(u.getParentId())
                .name(u.getName())
                .title(u.getTitle())
                .type(u.getType())
                .path(u.getPath())
                .component(u.getComponent())
                .perm(u.getPerm())
                .hide(u.getHide())
                .sort(u.getSort())
                .icon(u.getIcon())
                .redirect(u.getRedirect())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .alwaysShow(u.getAlwaysShow())
                .keepAlive(u.getKeepAlive())
                .full(u.getFull())
                .affix(u.getAffix())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param menuDomain 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected MenuBO revert(MenuDomain menuDomain) {
        return ObjectUtil.map(menuDomain, u -> MenuBO.builder()
                .id(u.getId())
                .parentId(u.getParentId())
                .title(u.getTitle())
                .name(u.getName())
                .type(u.getType())
                .path(u.getPath())
                .component(u.getComponent())
                .perm(u.getPerm())
                .hide(u.getHide())
                .sort(u.getSort())
                .icon(u.getIcon())
                .redirect(u.getRedirect())
                .createTime(u.getCreateTime())
                .updateTime(u.getUpdateTime())
                .alwaysShow(u.getAlwaysShow())
                .keepAlive(u.getKeepAlive())
                .full(u.getFull())
                .affix(u.getAffix())
                .build());
    }

    /**
     * MenuQueryBO 转 MenuQueryDomain
     *
     * @param condition MenuQueryDTO
     * @return MenuQueryBO
     */
    private MenuQueryDomain convertQuery(MenuQueryBO condition) {
        return ObjectUtil.map(condition, d -> MenuQueryDomain.builder()
                .keywords(d.getKeywords())
                .startTime(d.getStartTime())
                .endTime(d.getEndTime())
                .pageNo(d.getPageNo())
                .pageSize(d.getPageSize())
                .title(d.getTitle())
                .perm(d.getPerm())
                .status(d.getStatus())
                .build());
    }
}
